package com.wish.wishsystem_management.controller;


import com.wish.wishsystem_management.api.SysLog;
import com.wish.wishsystem_management.entity.SysDept;
import com.wish.wishsystem_management.entity.SysRole;
import com.wish.wishsystem_management.service.ISysDeptService;
import com.wish.wishsystem_management.service.ISysRoleDeptService;
import com.wish.wishsystem_management.service.ISysUserService;
import dto.systemDto.SysDeptDto;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import vo.ResultEntity;

import java.util.List;

/**
 * <p>
 * 部门管理 前端控制器
 * </p>
 *
 * @author zhaiwj
 * @since 2021-03-22
 */
@RestController
@RequestMapping("/sysDept")
public class SysDeptController {

    @Autowired
    private ISysDeptService iSysDeptService;
    @Autowired
    private ISysUserService iSysUserService;
    @Autowired
    private ISysRoleDeptService iSysRoleDeptService;

    @RequestMapping("list")
    public ResultEntity list() {
        List<SysDept> sysDepts = iSysDeptService.sysDeptListVo();
        return ResultEntity.ok("200", "部门列表查询成功", sysDepts);
    }

    @RequestMapping("editDept")
    @SysLog("保存部门")
    public ResultEntity edit(@RequestBody SysDeptDto sysDeptDto) {
        SysDept sysDept = new SysDept();
        BeanUtils.copyProperties(sysDeptDto, sysDept);
        if (sysDept.getDeptId() != null && sysDept.getDeptId() > 0) {
            iSysDeptService.updateById(sysDept);
            return ResultEntity.ok("200", "修改成功", "");
        } else {
            iSysDeptService.save(sysDept);
            return ResultEntity.ok("200", "添加成功", "");
        }
    }

    @RequestMapping("deleteDept")
    @SysLog("删除部门")
    public ResultEntity DeleteDept(Long id) {
        List<Long> longs = iSysDeptService.queryDetpIdList(id);
        if (longs.size() > 0) {
            return ResultEntity.error("402", "请先删除子部门", "");
        }

        List<SysRole> sysRoles = iSysRoleDeptService.queryListByDeptId(id);
        if (sysRoles.size() > 0) {
            return ResultEntity.error("401", "请先删除子角色", "");
        }
        /**
         * long转int  向下转型 容易栈溢出
         *  int ids = Integer.parseInt(String.valueOf(id));
         */

        iSysDeptService.removeById(id);
        return ResultEntity.ok(1);

    }




















    @RequestMapping("findByDeptId")
    public ResultEntity ResultEntity(Long id) {
        SysDept sysDept = iSysDeptService.findByDeptId(id);
        return ResultEntity.ok("200", "查询成功", sysDept);
    }

}

